#include <stdio.h>
#include <stdlib.h>


/*人机猜数游戏（2）*/
int a[4],flag,count;
void prt(void);
void bhdy(int s,int b);

void main ()
{
    int b1,b2,i,j,k=0,p,c;
    printf("Game begin!\n");
    printf("Now guess your number in mind is ####.\n");
    for(i=1;i<10&&k<4;i++)
    {
        printf("No.%d:your number may be:%d%d%d%d\n",++count,i,i,i,i);
        printf("How many digits have had correctly guessed: ");
        scanf("%d",&p);
        for(j=0;j<p;j++)
            a[k+j]=i;
            k+=p;
    }
    if(k<4)
        for(j=k;j<4;j++)
        a[i]=0;
        i=0;
        printf("N0. %d:your number may be: %d%d%d%d\n",++count,a[0],a[1],a[2],a[3]);
        printf("How many are in exact positions : ");
        scanf("%d",&b1);
        if(b1==4) {prt(); exit(0);}
            for(flag=1,j=0;j<3&&flag;j++)
                for(k=j+1;k<4&&flag;k++)
                    if(a[j]!=a[k])
                    {
                        c=a[j]; a[j]=a[k]; a[k]=c;
                        printf("No.%d: you number may be: %d%d%d%d\n",++count,a[0],a[1],a[2],a[3]);
                        printf("How many are in exact positons:");
                        scanf("%d",&b2);
                        if(b2==4) {prt(); flag=0;}
                        else if (b2-b1==2) bhdy(j,k);
                        else if(b2-b1==-2) 
                        {
                            c=a[i];a[i]=a[k];a[k]=c;
                            bhdy(j,k);
                        }
                        else if(b2<=b1)
                        {
                            c=a[i];a[i]=a[k];a[k]=c;
                        }
                        else b1=b2;
                    }
                    if(flag) printf("You input error!\n");
}

void prt(void)
{
    printf("Now your number must be %d%d%d%d\n",a[0],a[1],a[2],a[3]);
    printf("Game over\n");

}

void bhdy(int s,int b)
{
    int i,c=0,d[2];
    for(i=0;i<4;i++)
        if(i!=s&&i!=b) d[c++]=i;
        i=a[d[1]];a[d[1]]=a[d[0]]; a[d[0]]=i;
        prt();
        flag = 0;
}